﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain.MainModule.Entities;
using Infraestructure.CrossCutting.IoC;
using Infraestructure.CrossCutting.Logging;
using DistributedServices.Core.ErrorHandlers;
using System.ServiceModel;
using DistributedServices.MainModule.DTO;
using Application.MainModule.GroupBranchesManagement;


namespace DistributedServices.MainModule
{
    public partial class MainModuleService
    {
        public GroupBranch GetGroupBranchByGroupBrachId(Guid groupBranchId)
        {
            try
            {
                IGroupBranchManagementService groupBranchService = IoCFactory.Instance.CurrentContainer.Resolve<IGroupBranchManagementService>();

                return groupBranchService.FindGroupBranchByGroupBranchId(groupBranchId);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public void ChangeGroupBranch(GroupBranch groupBranch)
        {
            try
            {
                IGroupBranchManagementService groupBranchService = IoCFactory.Instance.CurrentContainer.Resolve<IGroupBranchManagementService>();
                groupBranchService.ChangeGroupBranch(groupBranch);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }

        }

        public void RemoveGroupBranch(GroupBranch groupBranch)
        {
            try
            {
                IGroupBranchManagementService groupBranchService = IoCFactory.Instance.CurrentContainer.Resolve<IGroupBranchManagementService>();
                groupBranchService.RemoveGroupBranch(groupBranch);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public void AddGroupBranch(GroupBranch groupBranch)
        {
            try
            {
                IGroupBranchManagementService groupBranchService = IoCFactory.Instance.CurrentContainer.Resolve<IGroupBranchManagementService>();
                groupBranchService.AddGroupBranch(groupBranch);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public List<GroupBranch> GetPagedGroupBranch(PagedCriteria pagedCriteria)
        {
            try
            {
                IGroupBranchManagementService groupBranchService = IoCFactory.Instance.CurrentContainer.Resolve<IGroupBranchManagementService>();

                return groupBranchService.FindPagedGroupBranches(pagedCriteria.PageIndex, pagedCriteria.PageCount);
            }
            catch (ArgumentException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
            catch (NullReferenceException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public BillingInformation GetBillingInformationByBillingInformationId(Guid billingInformationId)
        {
            try
            {
                IGroupBranchManagementService groupBranchService = IoCFactory.Instance.CurrentContainer.Resolve<IGroupBranchManagementService>();

                return groupBranchService.FindBillingInformationById(billingInformationId);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public List<BillingInformation> GetBillingInformationsByGroupBranchId(Guid groupBranchId)
        {
            try
            {
                IGroupBranchManagementService groupBranchService = IoCFactory.Instance.CurrentContainer.Resolve<IGroupBranchManagementService>();

                return groupBranchService.FindBillingInformationByGroupBranch(groupBranchId);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }

        }

        public List<BillingInformation> GetBillingInformationsByName(string name)
        {
            try
            {
                IGroupBranchManagementService groupBranchService = IoCFactory.Instance.CurrentContainer.Resolve<IGroupBranchManagementService>();

                return groupBranchService.FindBillingInformation(name, false);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }

        }

        public List<BillingInformation> GetBillingInformationsByCUIT(string cuit)
        {
            try
            {
                IGroupBranchManagementService groupBranchService = IoCFactory.Instance.CurrentContainer.Resolve<IGroupBranchManagementService>();

                return groupBranchService.FindBillingInformation(cuit, true);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }

        }

    }
}
